(function () {
	function  formatDate(now)   {
		var   year=now.getFullYear();
		var   month=now.getMonth()+1;
		var   date=now.getDate();
		var   hour=now.getHours();
		var   minute=now.getMinutes();
		var   second=now.getSeconds();
		return  year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
	}
	require.config({
		packages:[{
			name:'echarts',
			location:'/urltrace/js/echarts',
			main:'echarts'
		}
		]
	});
	/*$(".echarts-tooltip").live("hover",function(){
		$(this).show();
	});*/
	//按由大到小排序
	function getSortFun(order, sortBy) {
		var sortFun;
		if(order=="asc"){
			sortFun = new Function('a', 'b', 'return a.' + sortBy + '-' + 'b.' + sortBy);
		}else{
			sortFun = new Function('a', 'b', 'return b.' + sortBy + '-' + 'a.' + sortBy);
		}
		return sortFun;
	}
	var formatTime = function(time, format){
		var t = new Date(time);
		var tf = function(i){return (i < 10 ? '0' : '') + i};
		return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
			switch(a){
				case 'yyyy':
					return tf(t.getFullYear());
					break;
				case 'MM':
					return tf(t.getMonth() + 1);
					break;
				case 'mm':
					return tf(t.getMinutes());
					break;
				case 'dd':
					return tf(t.getDate());
					break;
				case 'HH':
					return tf(t.getHours());
					break;
				case 'ss':
					return tf(t.getSeconds());
					break;
			}
		})
	}
	//事件河流图
	function initEventRiverChart(elementId,mydata){
		var option = {
			title : {
				text: ''
			},
			tooltip : {
				trigger: 'item',
				enterable: true,
				backgroundColor:"rgba(153, 153, 153)",
				opacity:0.3,
				textStyle:{
					color:"#fff",
					fontSize:14,
					fontFamily:"微软雅黑",
					align:"left",
					marginLeft:"20px"
				},
				formatter : function(params){
					var str = "";
					str+=params[1]+"<br>";
					var arr = params[2].evolution; //数组
					str+=arr[0].time+"--"+arr[arr.length-1].time+"<br>";
					var count = 0;
					for(var i = 0;i<arr.length;i++){
						var emN = arr[i].value-0.1;
						count = count+emN;
					}
					str+=count+"条新闻";
					return str;
				}
			},
			/*legend: {
				data:['新闻事件']
			},*/
			toolbox: {
				show : true,
				padding:85,
				y:10,
				itemSize:26,
				color:['#222222'],
				feature : {
					mark : {show: false},
					dataView : {show: false},
					restore : {show: false},
					saveAsImage :{
						show : true,
						title : '导出为图片',
						icon:'../css/images/export.png',
						type : 'jpeg',
						lang : ['点击保存']

					}
				},
				textStyle:{
					color:"#222",
					fontFamily:"微软雅黑"
				}
			},
			grid:{
				borderWidth: 0,
				borderColor:"#fff"
			},
			xAxis : [
				{
					type : 'time',
					//show:false,
					boundaryGap: [0.05,0.1],
					splitLine:{
						show:false
					},
					scale:true
				}
			],
			series : [
				{
					name: "新闻事件",
					type: "eventRiver",
					weight: 100,
					itemStyle:{
						normal:{
							color:"#8e99b5",
							label:{
								show:true,
								textStyle:{
									color:"#222",
									fontSize:14,
									fontFamily:"微软雅黑"
								}
							}
						},
						emphasis: {
							label: {
								show:false
							}
						}
					},
					markPoint : {
						symbolSize: 5,       // 标注大小，半宽（半径）参数，当图形为方向或菱形则总宽度为symbolSize * 2
						itemStyle: {
							normal: {
								borderColor: '#87cefa',
								borderWidth: 1,            // 标注边线线宽，单位px，默认为1
								label: {
									show: false
								}
							},
							emphasis: {
								borderColor: '#1e90ff',
								borderWidth: 5,
								label: {
									show: false
								}
							}
						},
						//data : (setMarkPointData)(mydata,elementId)
						data : [
							//{name:"eeeeee第三方第三方",value:"100"}
							{type : 'max', name: '最大值',value:"120"},
							{type : 'min', name: '最小值',value:"20"}
						]
					},
					data: (setData)(mydata,elementId)
				}
			]
		};
		require(
			[
				'echarts',
				'echarts/chart/eventRiver'
			],
			function(ec) {
				var myChart = ec.init(document.getElementById(elementId));
				myChart.setOption(option);
				window.onresize = myChart.resize;
			}
		);
	}
	
	
//	var setMarkPointData = function(mydata,elementId){
//		var dataArr = [];
//		if(elementId=='amounts_chart'){
//			var myEventData = mydata.event;
//			for(var i=0;i<myEventData.length;i++){
//				var dataObj = {};
//				dataObj.name = myEventData[i].time;
//				dataObj.value = parseInt(myEventData[i].value);
//				dataArr.push(dataObj);
//			}
//			return dataArr;
//		}
//	}
	var setData=function (mydata,elementId) {
		var dataArr = [];
//		if(elementId=='amounts_chart'){
//			var dataObj = {};
//			var evolutionArr = [];
//			dataObj.name = mydata.list[0].title;
//			for(var i = 0;i<mydata.list.length;i++){
//				if(mydata.list[i].title!="虚拟节点"){
//					dataObj.name = mydata.list[i].title;
//					break;
//				}
//			}
//			dataObj.weight = 123;
//			var myEventData = mydata.event;
//			if(myEventData.length<=1) return dataArr;
//			for(var i=0;i<myEventData.length;i++){
//				var eventObj = {};
//				eventObj.time = myEventData[i].time;
//				eventObj.value = parseFloat(myEventData[i].value-1+0.1);
//				eventObj.detail={
//						text:'数值：'+(myEventData[i].value-1)
//				};
//				evolutionArr.push(eventObj);
//				dataObj.evolution = evolutionArr;
//			}
//			dataArr.push(dataObj);
//			return dataArr;
//		}else{
			var myEventDatas = mydata.list;
			var arrDemo = new Array();
			for(var j=0;j<10&&j<myEventDatas.length;j++){
				arrDemo.push(myEventDatas[j].event[0].time);
			}
			//arrDemo.sort(function(a,b){return a>b?1:-1});////从小到大排序
			for(var j=0;j<10&&j<myEventDatas.length;j++){
				
				for(var i=0;i<10&&i<myEventDatas.length;i++){
					var myEventData = mydata.list[i].event;
//					if(myEventData.length<=1)continue;
					if(arrDemo[j]==myEventData[0].time){
						var dataObj = {};
						dataObj.name = mydata.list[j].list[0].title;
						dataObj.weight = 200;
						var evolutionArr = [];
						for(var ii=0;ii<myEventData.length;ii++){
							var eventObj = {};
							eventObj.time = myEventData[ii].time;
							var numvalue=myEventData[ii].value-1+0.1;
							eventObj.value = numvalue;
								eventObj.detail={
										text:'数值：'+(myEventData[ii].value-1)
								};
							evolutionArr.push(eventObj);
							dataObj.evolution = evolutionArr;
						}
						dataArr.push(dataObj);
						break;
					}
				}
				
			}
			//console.log(JSON.stringify(dataArr));
			return dataArr;
//		}
		
	}
	
	window["initEventRiverChart"] = initEventRiverChart;
})()